﻿<Window x:Class="JenkinsController.View.JobView"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:controls="clr-namespace:JenkinsController.View.Controls"
        xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
        Title="{Binding Job.Name}" Height="300" Width="600" Name="Root">
    <DockPanel LastChildFill="True">

        <Menu DockPanel.Dock="Top">
            <MenuItem Header="Job">
                <MenuItem Header="Run" Command="{Binding Main.RunJobCommand, Source={StaticResource Locator}}"
                          CommandParameter="{Binding Job}">
                    <MenuItem.Icon>
                        <Image Width="16" Height="16" Source="..\Resources\Play.ico" />
                    </MenuItem.Icon>
                </MenuItem>

                <MenuItem Header="Stop" Command="{Binding Main.StopJobCommand, Source={StaticResource Locator}}"
                          CommandParameter="{Binding Job}">
                    <MenuItem.Icon>
                        <Image Width="16" Height="16" Source="..\Resources\Stop.ico" />
                    </MenuItem.Icon>
                </MenuItem>
            </MenuItem>
        </Menu>
        
        <ToolBarTray DockPanel.Dock="Top">
            <ToolBar>
                <Button Command="{Binding Main.RunJobCommand, Source={StaticResource Locator}}" CommandParameter="{Binding Job}"
                        ToolTip="Start a new build on job.">
                    <StackPanel Orientation="Horizontal">
                        <Image Width="10" Height="10" Source="..\Resources\Play.ico" />
                        <TextBlock Margin="5,0,0,0">Run</TextBlock>
                    </StackPanel>
                </Button>
                <Button Command="{Binding Main.StopJobCommand, Source={StaticResource Locator}}" CommandParameter="{Binding Job}"
                        ToolTip="Stop running build of job.">
                    <StackPanel Orientation="Horizontal">
                        <Image Width="10" Height="10" Source="..\Resources\Stop.ico" />
                        <TextBlock Margin="5,0,0,0">Stop</TextBlock>
                    </StackPanel>
                </Button>
                <Separator />
                <Button Command="{Binding Main.ChangeSetCommand, Source={StaticResource Locator}}" CommandParameter="{Binding SelectedBuild}"
                        ToolTip="Show source controller change set of job last build.">
                    <StackPanel Orientation="Horizontal">
                        <Image Width="16" Height="16" Source="..\Resources\ChangeSet.ico" />
                        <TextBlock Margin="5,0,0,0">Change set</TextBlock>
                    </StackPanel>
                </Button>
                <Button Command="{Binding Main.LogsCommand, Source={StaticResource Locator}}" CommandParameter="{Binding SelectedBuild}"
                        ToolTip="Show live logs of job last build.">
                    <StackPanel Orientation="Horizontal">
                        <Image Width="16" Height="16" Source="..\Resources\Logs.ico" />
                        <TextBlock Margin="5,0,0,0">Logs</TextBlock>
                    </StackPanel>
                </Button>
            </ToolBar>
        </ToolBarTray>

        <ListView ItemsSource="{Binding Job.Builds}" SelectedItem="{Binding SelectedBuild, Mode=TwoWay}">

            <ListView.ContextMenu>
                <ContextMenu>
                    <MenuItem Header="Change set" Command="{Binding Main.ChangeSetCommand, Source={StaticResource Locator}}"
                                  CommandParameter="{Binding SelectedBuild}">
                        <MenuItem.Icon>
                            <Image Width="16" Height="16" Source="..\Resources\ChangeSet.ico" />
                        </MenuItem.Icon>
                    </MenuItem>
                    <MenuItem Header="Logs" Command="{Binding Main.LogsCommand, Source={StaticResource Locator}}"
                                  CommandParameter="{Binding SelectedBuild}">
                        <MenuItem.Icon>
                            <Image Width="16" Height="16" Source="..\Resources\Logs.ico" />
                        </MenuItem.Icon>
                    </MenuItem>
                </ContextMenu>
            </ListView.ContextMenu>
            
            <ListView.View>
                
                <GridView>
                    <GridViewColumn Header="Status">
                        <GridViewColumn.CellTemplate>
                            <DataTemplate>
                                <controls:BuildStatusControl Status="{Binding Status}"
                                                             IsBuilding="{Binding IsBuilding}" />
                            </DataTemplate>
                        </GridViewColumn.CellTemplate>
                    </GridViewColumn>

                    <GridViewColumn Header="#" DisplayMemberBinding="{Binding DisplayName}" Width="200" />
                    <GridViewColumn Header="Date" DisplayMemberBinding="{Binding TimeStamp}" Width="150" />
                    <GridViewColumn Header="Duration"
                                    DisplayMemberBinding="{Binding Duration, StringFormat={}{0:mm\\:ss}}" Width="80" />

                    <GridViewColumn Header="Artifacts" Width="80">
                        <GridViewColumn.CellTemplate>
                            <DataTemplate>

                                <Border BorderBrush="DarkBlue" BorderThickness="0,0,0,1">
                                    <Border.Style>
                                        <Style TargetType="Border">
                                            <Style.Triggers>
                                                <DataTrigger Binding="{Binding Artifacts}" Value="{x:Null}">
                                                    <Setter Property="Visibility" Value="Collapsed" />
                                                </DataTrigger>
                                                <DataTrigger Binding="{Binding Artifacts.Length}" Value="0">
                                                    <Setter Property="Visibility" Value="Collapsed" />
                                                </DataTrigger>
                                            </Style.Triggers>
                                        </Style>
                                    </Border.Style>

                                    <i:Interaction.Triggers>
                                        <i:EventTrigger EventName="MouseLeftButtonDown">
                                            <i:InvokeCommandAction
                                                Command="{Binding DataContext.DownloadArtifactsCommand, ElementName=Root}"
                                                CommandParameter="{Binding}" />
                                        </i:EventTrigger>
                                    </i:Interaction.Triggers>

                                    <TextBlock
                                        Text="{Binding Artifacts.Length, StringFormat={}{0} artifacts}"
                                        Foreground="DarkBlue" Cursor="Hand"
                                        ToolTip="Click here to download all artifacts." />
                                </Border>

                            </DataTemplate>
                        </GridViewColumn.CellTemplate>
                    </GridViewColumn>
                </GridView>
            </ListView.View>

        </ListView>

    </DockPanel>
</Window>